<?php
class AccController extends Epic_Controller_Action 
{	
	/**
	 * Authentication
	 *
	 * @see Epic_Controller_Action::init()
	 * @return void;
	 */

	public function loginAction () {
		Epic::setTitle("Login");
  		//Epic::addListCssUrl('admin/login.css');
		Epic::addListCssUrl('admin/screen.css');
		Epic::addListJsUrl('admin/login/custom_jquery.js');
		Epic::addListJsUrl('admin/login/jquery.pngFix.pack.js');
  		if ($data = $this->getRequest()->getPost()) {
  			$username = $data['username'];
  			$password = $data['password'];
  			if ($username=="") {
  				Epic::setError("Please enter the UserName");  				
  			} else if ($password == ""){
  				Epic::setError("Please enter the password");
  			}
  			else {
				$user = Epic::getModel('admin/admin')->load($username, 'username');
				if(!is_null($user)) {
					if($user['password'] == $password) {
						$storeUserSession = array(
							'access' => true,
							'admin' => true,
							'id_admin' => $user['id_admin'],
							'username' => $user['username'],
							'start' => time());
						Epic::getSession()->set($storeUserSession);
						$this->_redirect(Epic::getUrl('admin/journal/list'));
					} else {
						Epic::setError("The Username or Passworld is incorrect");
					}
				} else {
					Epic::setError("The Username or Passworld is incorrect");
				}
  			}
		}
  	}
	public function logoutAction()
	{
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
		Epic::getSession()->destroySession();
		$this->_redirect(Epic::getUrl('admin/acc/login'));
	}
	
	public function listUserAction (){
		Epic::setTitle("List User");
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
			
		Epic::addListJsUrl('admin/table/jquery.dataTables.min.js');
  		Epic::addListJsUrl('admin/table/table.js');
  		Epic::addListJsUrl('admin/ajax.js');
  		
	}
	
	public function listEmployeeAction (){
		Epic::setTitle("List User");
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
			
		Epic::addListJsUrl('admin/table/jquery.dataTables.min.js');
  		Epic::addListJsUrl('admin/table/table.js');
  		Epic::addListJsUrl('admin/ajax.js');
  		
	}
	
	public function addEmployeeAction (){
		Epic::setTitle("Add Employ");
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
		if ($data=$this->getRequest()->getPost()){
			if($data['username']==""){
				Epic::setError("Please enter the User Name");				
			}else if($data['password']==""){
				Epic::setError("Please enter the PassWord");
			}else if($data['email']=""){
				Epic::setError("Please enter the Email");
			}else if($data['first_name']==""){
				Epic::setError("Please enter the First Name");
			}else if($data['last_name']==""){
				Epic::setError("Please enter the Last Name");
			}
			else{
				Epic::getModel('admin/employee')->save($data);
				Epic::setSuccess('The Employee has been saved.');
				$this->_redirect (Epic::getUrl ('admin/acc/listEmployee'));//luu xong chuyen sang trang listEmployee
			}
		}
	}
}